package fragmentson;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import java.util.ArrayList;

import activity.SonActivity;
import bean.EntityForSimple;
import bean.MessageForSimpleList;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;
import util.adapter.DevicesPropertyAdapter;

/**
 * 设备属性选择页
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class DeviceProperty extends BackHandledFragment {

	private View rootView;
	private ListView lv_property;
	private DevicesPropertyAdapter adapter;
	private ArrayList<EntityForSimple> mDatas = new ArrayList<>();
    //brandid 品牌id用来筛选颜色和型号
	private String type = "",brandid;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_devicepropertylist, null);
		type = getArguments().getString("type");
        brandid = getArguments().getString("brandid");
		initView();
		initData();
		return rootView;
	}

	private void initData(){
		if(type.equals("1")) {
			ApiUtil.getApiService().shopList(DemoApplication.getToken()).enqueue(new Callback<MessageForSimpleList>() {
				@Override
				public void onResponse(Call<MessageForSimpleList> call, Response<MessageForSimpleList> response) {
					try {
						MessageForSimpleList entity = response.body();
						if (entity.getCode().equals("10008")) {
							mDatas = entity.getData();
							adapter = new DevicesPropertyAdapter(getActivity(), mDatas);
							lv_property.setAdapter(adapter);
							lv_property.setOnItemClickListener(new AdapterView.OnItemClickListener() {
								@Override
								public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
									mcall.listener(((TextView) view.findViewById(R.id.tv_name)).getText().toString(),mDatas.get(position).getId());
									((SonActivity) getActivity()).backTask();
								}
							});
						} else {
							Toast.makeText(getActivity(), entity.getMsg(), Toast.LENGTH_LONG).show();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				@Override
				public void onFailure(Call<MessageForSimpleList> call, Throwable t) {
					Toast.makeText(getActivity(), getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
				}
			});
		}else if(type.equals("2")){
			ApiUtil.getApiService().brandList(DemoApplication.getToken()).enqueue(new Callback<MessageForSimpleList>() {
				@Override
				public void onResponse(Call<MessageForSimpleList> call, Response<MessageForSimpleList> response) {
					try {
						MessageForSimpleList entity = response.body();
						if (entity.getCode().equals("10008")) {
							mDatas = entity.getData();
							adapter = new DevicesPropertyAdapter(getActivity(), mDatas);
							lv_property.setAdapter(adapter);
							lv_property.setOnItemClickListener(new AdapterView.OnItemClickListener() {
								@Override
								public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
									mcall.listener(((TextView) view.findViewById(R.id.tv_name)).getText().toString(),mDatas.get(position).getId());
									((SonActivity) getActivity()).backTask();
								}
							});
						} else {
							Toast.makeText(getActivity(), entity.getMsg(), Toast.LENGTH_LONG).show();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				@Override
				public void onFailure(Call<MessageForSimpleList> call, Throwable t) {
					Toast.makeText(getActivity(), getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
				}
			});
		}
        else if(type.equals("3")){
            ApiUtil.getApiService().brandList(DemoApplication.getToken()).enqueue(new Callback<MessageForSimpleList>() {
                @Override
                public void onResponse(Call<MessageForSimpleList> call, Response<MessageForSimpleList> response) {
                    try {
                        MessageForSimpleList entity = response.body();
                        if (entity.getCode().equals("10008")) {
                            EntityForSimple mData = new EntityForSimple();
                            for(EntityForSimple data:entity.getData()){
                                if(data.getId().equals(brandid)){
                                    mData = data;
                                    ArrayList<EntityForSimple> mDatas = new ArrayList<EntityForSimple>();
                                    for(String name:mData.getModels()){
                                        EntityForSimple adata = new EntityForSimple();
                                        adata.setName(name);
                                        mDatas.add(adata);
                                    }
                                    adapter = new DevicesPropertyAdapter(getActivity(), mDatas);
                                    lv_property.setAdapter(adapter);
                                    lv_property.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                        @Override
                                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                            mcall.listener(((TextView) view.findViewById(R.id.tv_name)).getText().toString(),"");
                                            ((SonActivity) getActivity()).backTask();
                                        }
                                    });
                                    return;
                                }
                            }

                        } else {
                            Toast.makeText(getActivity(), entity.getMsg(), Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<MessageForSimpleList> call, Throwable t) {
                    Toast.makeText(getActivity(), getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
                }
            });
        }
        else if(type.equals("4")){
            ApiUtil.getApiService().brandList(DemoApplication.getToken()).enqueue(new Callback<MessageForSimpleList>() {
                @Override
                public void onResponse(Call<MessageForSimpleList> call, Response<MessageForSimpleList> response) {
                    try {
                        MessageForSimpleList entity = response.body();
                        if (entity.getCode().equals("10008")) {
                            EntityForSimple mData = new EntityForSimple();
                            for(EntityForSimple data:entity.getData()){
                                if(data.getId().equals(brandid)){
                                    mData = data;
                                    ArrayList<EntityForSimple> mDatas = new ArrayList<EntityForSimple>();
                                    for(String name:mData.getColores()){
                                        EntityForSimple adata = new EntityForSimple();
                                        adata.setName(name);
                                        mDatas.add(adata);
                                    }
                                    adapter = new DevicesPropertyAdapter(getActivity(), mDatas);
                                    lv_property.setAdapter(adapter);
                                    lv_property.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                        @Override
                                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                            mcall.listener(((TextView) view.findViewById(R.id.tv_name)).getText().toString(),"");
                                            ((SonActivity) getActivity()).backTask();
                                        }
                                    });
                                    return;
                                }
                            }

                        } else {
                            Toast.makeText(getActivity(), entity.getMsg(), Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<MessageForSimpleList> call, Throwable t) {
                    Toast.makeText(getActivity(), getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
                }
            });
        }
        else if(type.equals("5")){
            ApiUtil.getApiService().propertyList(DemoApplication.getToken()).enqueue(new Callback<MessageForSimpleList>() {
                @Override
                public void onResponse(Call<MessageForSimpleList> call, Response<MessageForSimpleList> response) {
                    try {
                        MessageForSimpleList entity = response.body();
                        if (entity.getCode().equals("10008")) {
                            mDatas = entity.getData();
                            adapter = new DevicesPropertyAdapter(getActivity(), mDatas);
                            lv_property.setAdapter(adapter);
                            lv_property.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    mcall.listener(((TextView) view.findViewById(R.id.tv_name)).getText().toString(),mDatas.get(position).getId());
                                    ((SonActivity) getActivity()).backTask();
                                }
                            });
                        } else {
                            Toast.makeText(getActivity(), entity.getMsg(), Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<MessageForSimpleList> call, Throwable t) {
                    Toast.makeText(getActivity(), getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
                }
            });
        }
        else if(type.equals("6")){
            ApiUtil.getApiService().policeList(DemoApplication.getToken()).enqueue(new Callback<MessageForSimpleList>() {
                @Override
                public void onResponse(Call<MessageForSimpleList> call, Response<MessageForSimpleList> response) {
                    try {
                        MessageForSimpleList entity = response.body();
                        if (entity.getCode().equals("10008")) {
                            mDatas = entity.getData();
                            adapter = new DevicesPropertyAdapter(getActivity(), mDatas);
                            lv_property.setAdapter(adapter);
                            lv_property.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    mcall.listener(((TextView) view.findViewById(R.id.tv_name)).getText().toString(),mDatas.get(position).getId());
                                    ((SonActivity) getActivity()).backTask();
                                }
                            });
                        } else {
                            Toast.makeText(getActivity(), entity.getMsg(), Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<MessageForSimpleList> call, Throwable t) {
                    Toast.makeText(getActivity(), getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
                }
            });
        }
        else if(type.equals("7")){
            ApiUtil.getApiService().producerList(DemoApplication.getToken()).enqueue(new Callback<MessageForSimpleList>() {
                @Override
                public void onResponse(Call<MessageForSimpleList> call, Response<MessageForSimpleList> response) {
                    try {
                        MessageForSimpleList entity = response.body();
                        if (entity.getCode().equals("10008")) {
                            mDatas = entity.getData();
                            adapter = new DevicesPropertyAdapter(getActivity(), mDatas);
                            lv_property.setAdapter(adapter);
                            lv_property.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    mcall.listener(((TextView) view.findViewById(R.id.tv_name)).getText().toString(),mDatas.get(position).getId());
                                    ((SonActivity) getActivity()).backTask();
                                }
                            });
                        } else {
                            Toast.makeText(getActivity(), entity.getMsg(), Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<MessageForSimpleList> call, Throwable t) {
                    Toast.makeText(getActivity(), getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
                }
            });
        }
	}

	public void initView(){
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.title_mycar);
		lv_property = ((ListView) rootView.findViewById(R.id.lv_property));
	}


	public static DeviceProperty newInstance(CallBackListener call,String type,String brandid) {
		DeviceProperty fragment = new DeviceProperty();
		mcall = call;
		Bundle bundle = new Bundle();
		bundle.putString("type",type);
        bundle.putString("brandid",brandid);
		fragment.setArguments(bundle);
		return fragment;
	}


	@Override
	public boolean onBackPressed() {
		return false;
	}

	public static interface CallBackListener{
		void listener(String item,String id);
	}
	private static CallBackListener mcall;
	
}
